package com.loan.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.system.domain.ApplyEntity;
import com.loan.system.domain.PayConfigEntity;
import com.loan.system.domain.UserVaEntity;
import com.loan.system.domain.req.AccountEntity;
import com.loan.system.mapper.UserVaMapper;
import com.loan.system.service.UserVaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author shiguang on 2022/9/22 15:53
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class UserVaServiceImpl implements UserVaService {

    private final UserVaMapper userVaMapper;

    @Override
    public void saveUserVa(String data, ApplyEntity applyEntity, PayConfigEntity payConfigEntity) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONObject vaInfo = jsonObject.getJSONObject("vaInfo");
        UserVaEntity entity = new UserVaEntity();
        entity.setUserId(applyEntity.getUserId());
        entity.setApplyId(applyEntity.getId());
        entity.setAccountName(vaInfo.getString("accountName"));
        entity.setAccountNo(vaInfo.getString("accountNo"));
        entity.setBank(vaInfo.getString("bank"));
        entity.setSign(vaInfo.getString("sign"));
        entity.setPayConfigId(payConfigEntity.getId());
        entity.setCreateTime(new Date());
        entity.setType("1");
        userVaMapper.insert(entity);
    }

    @Override
    public UserVaEntity selectByNoAndName(String accountNo, String accountName) {
       return userVaMapper.selectOne(new QueryWrapper<UserVaEntity>().eq("account_no",accountNo).eq("account_name",accountName));
    }

    @Override
//    @Async
    public void saveUserVaHqpay(JSONObject data, ApplyEntity applyEntity, PayConfigEntity payConfigEntity) {
        UserVaEntity entity = new UserVaEntity();
        entity.setUserId(applyEntity.getUserId());
        entity.setApplyId(applyEntity.getId());
        entity.setAccountName(data.getString("cardholder_name"));
        entity.setAccountNo(data.getString("card_number"));
        entity.setBank(data.getString("card_id"));
        entity.setPayConfigId(payConfigEntity.getId());
        entity.setCreateTime(new Date());
        entity.setExpiredTime(data.getDate("expired_time"));
        entity.setRequestId(data.getString("request_id"));
        entity.setMerchantId(data.getString("merchant_id"));
        userVaMapper.insert(entity);
    }

    @Override
    public UserVaEntity selectByCardId(String cardId) {
        return userVaMapper.selectOne(new QueryWrapper<UserVaEntity>().eq("account_no",cardId).last("limit 1"));
    }

    @Override
    public UserVaEntity selectByBank(String account) {
        return userVaMapper.selectOne(new QueryWrapper<UserVaEntity>().eq("bank",account).orderByDesc("id").last("limit 1"));
    }

    @Override
    public UserVaEntity getUserVaByApplyId(Long applyId) {
        return userVaMapper.selectOne(new QueryWrapper<UserVaEntity>().eq("apply_id",applyId).orderByDesc("id").last("limit 1"));
    }

    /**
     * 保存虚拟账户信息
     * @param data
     * @param applyEntity
     * @param payConfigEntity
     * @param batchId
     */
    @Override
    public void saveUserVaHqpayNew(AccountEntity data, ApplyEntity applyEntity, PayConfigEntity payConfigEntity, String batchId) {
        UserVaEntity entity = new UserVaEntity();
        entity.setUserId(applyEntity.getUserId());
        entity.setApplyId(applyEntity.getId());
        entity.setAccountName(data.getVaName());
        entity.setAccountNo(data.getVaNumber());
        entity.setBank(null);
        entity.setPayConfigId(payConfigEntity.getId());
        entity.setCreateTime(new Date());
        entity.setExpiredTime(null);
        entity.setRequestId(batchId);
        entity.setMerchantId(batchId);
        entity.setType("2");
        userVaMapper.insert(entity);
    }

    @Override
    public List<UserVaEntity> selectListByType(String type) {
        return userVaMapper.selectListByType(type);
    }
}
